//
//  HDNavigationController.m
//  OTIS_MD
//
//  Created by 何达达 on 2017/8/17.
//  Copyright © 2017年 hedada. All rights reserved.
//

#import "HDNavigationController.h"
#import "UIBarButtonItem+Extention.h"
@interface HDNavigationController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation HDNavigationController

+ (void)initialize {
    
    UIBarButtonItem *item  = [UIBarButtonItem appearance];
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置不可用状态
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
    //------------------------------------------------------------------------------------------------------------------------------------------------
    
    UINavigationBar *bar  = [UINavigationBar appearance];
    [bar setBarTintColor:RGB(30, 32, 81)];
    [bar setTintColor:[UIColor whiteColor]];
    [bar setTitleTextAttributes:disableTextAttrs];
    bar.translucent = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.interactivePopGestureRecognizer.delegate =  self;
    
    self.delegate = self;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.viewControllers.count <= 1 ) {
        return NO;
    }
    
    return YES;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    
    if (self.viewControllers.count == 1) {
      [self.navigationItem setHidesBackButton:YES];
    }else{
        
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"返回" Target:self anction:@selector(back:) image:@"return.png" highlightimage:@"return.png"];
    }
    [super pushViewController:viewController animated:YES];
    
}

-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    

    self.interactivePopGestureRecognizer.enabled = [self.viewControllers count] > 2 ;
        
  
}

-(void)dismissNav
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)back:(UIViewController *)viewController {
    
    
    [self popViewControllerAnimated:YES];
   
}






@end
